package com.nbcio.im.listener;

import com.nbcio.im.config.ImProperties;
import com.nbcio.im.service.ImService;
import com.nbcio.system.domain.SysUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * IM用户同步监听器
 * 负责自动同步用户数据到IM系统
 *
 * @author nbacheng
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ImUserSyncListener {

    private final ImService imService;
    private final ImProperties imProperties;

    /**
     * 应用启动完成后进行用户同步
     */
    @EventListener(ApplicationReadyEvent.class)
    @Async
    public void onApplicationReady() {
        if (imProperties.isEnabled() && imProperties.getUserSync().isAutoSync()) {
            log.info("开始同步用户到IM系统...");
            // 这里可以添加启动时的用户同步逻辑
        }
    }

    /**
     * 定时同步用户数据
     */
    @Scheduled(fixedRateString = "#{@imProperties.userSync.syncInterval * 60 * 1000}")
    public void scheduledUserSync() {
        if (imProperties.isEnabled() && imProperties.getUserSync().isAutoSync()) {
            log.info("开始定时同步用户到IM系统...");
            // 这里可以添加定时同步逻辑
            // 例如：同步新增或修改的用户
        }
    }
}
